//
//  KitchenManager.m
//  Kitchen
//
//  Created by   on 15/8/28.
//  Copyright (c) 2015年 雍玉虎. All rights reserved.
//

#import "KitchenManager.h"
#import "Kitchen.h"
#import "PopularDetailsInfo.h"
#import "DataYTool.h"


@implementation KitchenManager

static KitchenManager * s_defaultManager = nil;

+ (KitchenManager *)defaultManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        s_defaultManager = [self new];
    });
    
    return s_defaultManager;
}

/** 初始化可变数组rankingList */
- (instancetype)init
{
    self = [super init];
    if (self) {
        _rankingList = [NSMutableArray array];
        _XHLifeList = [NSMutableArray array];
    }
    return self;
}

- (void)GetKitchenInfoComplete:(void(^)())complete {

    dispatch_async(dispatch_queue_create(0, 0), ^{
        NSURL *url = [NSURL URLWithString:kitchenHome];
        //    3 创建请求对象,绑定URL
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
        //   4 创建一个相应对象
        NSURLResponse *response = nil;
        // 5
        NSError *error = nil;
        //    6 发送请求
        NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        //   拿到字典
        NSDictionary *dics = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//                    NSLog(@"%@",dics);
        self.RecommendList  = [NSMutableArray array];
        for (NSDictionary *dic in dics[@"content"][@"pop_recipe_lists"][@"recipe_lists"]) {
            RecommendKitchenInfo *recInfo = [RecommendKitchenInfo RecommendSetRecommendValuesFOrKeyWithDictionary:dic];
            [self.RecommendList  addObject:recInfo];
        }
        self.kitchenList = [NSMutableArray array];
        for (NSDictionary *dic in dics[@"content"][@"pop_events"][@"events"]) {
            self.KIntchenInfo = [KitchenInfo KitchenInfoSetEventsValuesFOrKeyWithDictionary:dic];
            [self.kitchenList addObject:self.KIntchenInfo];
        }
        self.rankingList = [NSMutableArray array];
        for (NSDictionary *dic in dics[@"content"][@"pop_lists"][@"lists"]) {
            RankingKitchenInfo *rankInfo = [RankingKitchenInfo RankingSetRecommendValuesFOrKeyWithDictionary:dic];
            [self.rankingList   addObject:rankInfo];
            
//            NSLog(@"大标题==%@",rankInfo.display_name);
//            NSLog(@"副标题==%@",rankInfo.desc);
//            NSLog(@"图片地址==%@",rankInfo.pic);

        }
        // 香哈生活圈
        [DataYTool solveDataWithURL:XiangHaLife andMethod:@"GET" andBody:nil andBlock:^(NSData *data) {
            self.XHLifeList = [NSMutableArray new];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            for (NSDictionary *dict in dic[@"data"]) {
                self.XHLifeInfo = [XiangHaLifeInfo XiangHaLifeSetXiangHaLifeValueForKeyWithDictionary:dict];
                [self.XHLifeList addObject:self.XHLifeInfo];
//                NSLog(@"%@",dic);
                
//                /** 打印标题 */
//                NSLog(@"%@",self.XHLifeInfo.title);
//                /** 打印头像 */
//                NSLog(@"%@",self.XHLifeInfo.imgShow);
//                /** 打印详情页面照片数组 */
//                NSLog(@"%@",self.XHLifeInfo.imgs);
//                /** 打印生活圈详情 */
//                NSLog(@"%@",self.XHLifeInfo.content);
//                /** 打印发布时间 */
//                 NSLog(@"%@",self.XHLifeInfo.nav);
                /** 打印图片数量 */
//                NSLog(@"%@",_XHLifeInfo.imgNum);
//                NSLog(@"-------------");
            }
        }];
        
        //香哈主页
        [DataYTool solveDataWithURL:XiangHaHome andMethod:@"GET" andBody:nil andBlock:^(NSData *data) {
            NSDictionary *dic    = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            self.XHHomeList = [NSMutableArray new];
            self.XHHomeInfo = [XiangHaHomeInfo XiangHaHomeSetXiangHaHomeInfoValueForKeyWithDictionary:dic[@"data"]];
            [self.XHHomeList addObject:self.XHHomeInfo];
//            NSLog(@"%@",self.XHHomeList);
        }];

        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (complete) {
                complete();
            }
        });
    });
}

- (void)GetPopularDetailsInfoCOmplete:(void(^)())complete {
    dispatch_async(dispatch_queue_create(0, 0), ^{
        NSURL *url = [NSURL URLWithString:PopularDetails];
        //    3 创建请求对象,绑定URL
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
        //   4 创建一个相应对象
        NSURLResponse *response = nil;
        // 5
        NSError *error = nil;
        //    6 发送请求
        NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        //   拿到字典
        NSDictionary *dics = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.popularDetailsList = [NSMutableArray array];
//        [self.popularDetailsInfo setValuesForKeysWithDictionary:dics[@"content"]];
        self.popularDetailsInfo = [PopularDetailsInfo popularDetailsSetPopularDetailValueForKeyWithDictionary:dics[@"content"]];
                [self.popularDetailsList addObject:self.popularDetailsInfo];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (complete) {
                complete();
            }
        });
    });
   
    
}







@end
